package com.javaex.book.controller;

import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.javaex.book.pojo.Book;
import com.javaex.book.services.BookService;
import com.javaex.book.utils.Pagination;
import com.javaex.book.utils.ReturnResult;
import com.javaex.book.utils.UploadUtils;

@Controller
@ResponseBody
@RequestMapping("/book")
public class BookController {
	
	private static final Logger logger = LoggerFactory.getLogger(BookController.class);

	private final BookService bookService;
	
	@Autowired
	public BookController(BookService bookService) {
		this.bookService = bookService;
	}
	
	/**
	 * 查询图书数据
	 * @param pageNo 页码
	 * @param pageSize 页容量
	 * @param minPrice 最低价
	 * @param maxPrice 最高价
	 * @return 图书数据
	 */
	@GetMapping()
	public Pagination bookList(Integer pageNo, Integer pageSize, BigDecimal minPrice, BigDecimal maxPrice) {
		logger.info("查询图书列表: [pageNo={}, pageSize={}, minPrice={}, maxPrice={}]", pageNo, pageSize, minPrice, maxPrice);
		Integer totalCount = bookService.countBooks(minPrice, maxPrice);
		Pagination pagination = new Pagination(pageNo, pageSize, totalCount);
		if (totalCount == 0) {
			return pagination;
		} 
		List<Book> bookList = bookService.getBookList(pagination.getStart(), pagination.getSize(), minPrice, maxPrice);
		pagination.setData(bookList);
		return pagination;
	}
	
	/**
	 * 根据图书id获取图书信息
	 * @param id 图书id
	 * @return 图书信息
	 */
	@GetMapping("/{id}")
	public Book getBookById(@PathVariable("id") Integer id) {
		logger.info("查询图书：[id={}]", id);
		return bookService.getBookById(id);
	}
	
	/**
	 * 添加图书
	 * @param book 图书信息
	 * @param image 上传的图片
	 */
	@PostMapping()
	public ReturnResult addBook(Book book, MultipartFile image, HttpServletRequest req) {
		// 处理上传图片
		ReturnResult result = UploadUtils.uploadImage(image, req);
		if (result.isFail()) {
			return result;
		}
		book.setImgPath(result.getData().toString());
		logger.info("添加图书：{}", book);
		return bookService.addBook(book);
	}
	
	/**
	 * 修改图书
	 * @param book 图书信息
	 * @param image 上传图片
	 */
	@PutMapping()
	public ReturnResult updateBook(Book book, MultipartFile image, HttpServletRequest req) {
		// 处理上传图片
		ReturnResult result = UploadUtils.uploadImage(image, req);
		if (result.isSuccess()) {
			book.setImgPath(result.getData().toString());
		}
		logger.info("修改图书：{}", book);
		return bookService.updateBook(book);
	}
	
	/**
	 * 删除图书
	 * @param id 图书id
	 * @return 删除结果
	 */
	@DeleteMapping("/{id}")
	public ReturnResult deleteBook(@PathVariable("id") Integer id) {
		logger.info("删除图书：[id={}]", id);
		return bookService.deleteBook(id);
	}
}
